<template>
  <div class="login">
    <div class="main">
      <el-form ref="user" :model="user" label-width="auto">
        <el-form-item
          prop="loginName"
          :rules="[
            { required: true, message: '请输入用户名', trigger: 'blur' },
            {
              message: '用户名长度必须为2-11位',
              trigger: ['blur', 'change'],
              pattern: /^.{2,11}$/,
            },
          ]"
        >

          <el-input
            v-model="user.loginName"
            prefix-icon="el-icon-mobile-phone"
            placeholder="请输入用户名"
          />

        </el-form-item>
        <el-form-item
          prop="password"
          :rules="[
            {
              required: true,
              message: '请输入密码',
              trigger: 'blur',
            },
            {
              message: '密码长度不能小于5位',
              trigger: ['blur', 'change'],
              pattern: /^.{5,}$/,
            },
          ]"
        >

          <el-input
            v-model="user.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          />

        </el-form-item>
        <el-form-item
          prop="code"
          class="el-input el-input--prefix code"
          :rules="[
            {
              required: true,
              message: '请输入验证码',
              trigger: 'blur',
            },
          ]"
        >
          <el-input
            v-model="user.code"
            maxlength="4"
            placeholder="请输入验证码"
            class="codeIpt"
            prefix-icon="el-icon-discover"
          />
          <img :src="url" alt="" @click="changePic">
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="submitForm('user')">登录</el-button>
        </el-form-item>
      </el-form>
      <div class="logo">
        <img src="../../assets/img/logo.595745bd.png" alt="">
      </div>
    </div>
  </div>
</template>
<script>
import { getCodePic, loginRequest } from '@/api/user'
export default {
  name: 'LoginPage',
  data() {
    return {
      // url: 'https://likede2-java.itheima.net/api/user-service/user/imageCode/G3nYzIYKCrvxgGc2T3UsizHAkLYXv7xR',
      url: '',
      user: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        clientToken: 'iNKMduB8B7OSRuccXsthWLbgxYMJKE3X',
        loginType: '0',
        mobile: '18155518348'
      }
      // clientToken: 'iNKMduB8B7OSRuccXsthWLbgxYMJKE3X'
    }
  },
  async created() {
    this.getCode()
  },
  methods: {
    // 点击图片
    changePic() {
      this.getCode()
    },
    async getCode() {
      const res = await getCodePic(this.user.clientToken)
      console.log(res, 7777)
      const arrayBuffer = res.data
      const blob = new Blob([arrayBuffer], { type: 'image/jpeg' })
      const imgUrl = URL.createObjectURL(blob)
      this.url = imgUrl
      console.log(this.url)
    },

    submitForm(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          // this.$message.success("登录成功");
          // 发送请求
          // console.log(this.user, 'ioi')
          try {
            const res = await loginRequest(this.user)
            console.log(res, 'login')
            this.$store.commit('user/getUserTokenMutation', res.data)
            console.log(this.$store.state.user.token, 9999999)
            // 页面跳转
            this.$router.push('/dashboard')
          } catch (error) {
            //
          }
        }
      })
    }

    // async submitForm() {
    //   const res = await getCodePic()
    //   console.log(res, 'code')
    // }
  }
}
</script>
<style lang='scss'  scoped>
.login {
  width: 100vw;
  height: 100vh;
  background-image: url(../../assets/img/background.be4fae7d.png);
  position: relative;

  .main {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 518px;
    height: 450px;
    background-color: #fff;
    border-radius: 10px;

    .el-form {
      width: 100%;
      margin-top: 114px;
      margin-bottom: 22px;
      padding: 0 20px;
    }

    .logo {
      width: 140px;
      height: 140px;
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%, -50%);
      border-radius: 50%;
      background-color: #eee;
      box-shadow: 5px 5px 5px #ccc;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
  }
}
</style>

<style lang="scss" >
.login{

  .el-form-item__content {
    height: 52px;

    .el-input__inner {
      height: 52px;
    }
  }

  .el-form-item.el-input.el-input--prefix.code {
    display: flex;
    position: relative;

    .codeIpt.el-input.el-input--prefix {
      width: 346px;

      .el-input__inner {
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
      }
    }

    img {
      position: absolute;
      border-top-right-radius: 4px;
      border-bottom-right-radius: 4px;
      height: 52px;
    }
  }

  .btn {
    width: 100%;

    .el-button {
      width: 100%;
      height: 52px;
      background-color: #5a69d9;
    }
  }
}
</style>
